package com.apes.scm.stock.transfer.service;

import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.account.account.model.AccountingMethod;
import com.apes.scm.account.account.model.InventoryMode;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.account.tax.service.TaxService;
import com.apes.scm.contract.model.Contract;
import com.apes.scm.contract.repository.ContractRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.service.PartyRoleService;
import com.apes.scm.stock.transfer.model.Transfer;
import com.apes.scm.stock.transfer.repository.TransferRepository;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 转储单生成凭证
 *
 * @author wuq
 * @create 2019-01-11 15:47
 */
@Service("pickFlowTransferVoucherService")
public class PickFlowTransferVoucherService {

    @Autowired
    TransferRepository transferRepository;

    @Autowired
    ContractRepository contractRepository;

    @Autowired
    PartyRoleService partyRoleService;

    @Autowired
    TaxService taxService;

    public Voucher flowTransferVoucher(SimpleRequest request) {
        Transfer transfer = request.getO(Transfer.class);

        PartyRole company = transfer.getOriginLocation().getCompany();
        PartyRole customerCompany = transfer.getReceiptLocation().getCompany();

        Voucher voucher = new Voucher();
        // 主表信息
        voucher.setCompany(customerCompany);
        voucher.setClerk(customerCompany);
        voucher.setPartner(company);

        voucher.setInventoryMode(InventoryMode.IM_OWN);
        voucher.setAccountingMethod(AccountingMethod.AM_PURCHASE);

        voucher.setScene(transfer.getScene());
        voucher.setOrigin(transfer.getId());
        voucher.setOriginBusinessType(transfer.getInnerBusinessType());

        // 获取合同中的部门
        PartyRole customer = partyRoleService.findByPartyAndRole( customerCompany.getParty().getId() ,"Customer" );
        List<Contract> contractList = contractRepository.findAllByCompanyAndPartner(company.getId(), customer.getId(), new Date());
        if ( contractList.size() > 0 ) {
            // todo 暂时没有判断是否为销售合同以及采购合同
            voucher.setDept( contractList.get(0).getDept() );
            voucher.setContract( contractList.get(0) );
        }

        // 由于转储单生成凭证不能使用 交货单，所以使用 转储单自己本身
        voucher.setSource(transfer.getId());
        voucher.setSourceBusinessType(transfer.getInnerBusinessType());
        
        // 从表信息
        voucher.setVoucherItems(getTransferVoucherItems(transfer));

        return voucher;
    }

    public Set<VoucherItem> getTransferVoucherItems(Transfer transfer) {
        Set<VoucherItem> voucherItems = new HashSet();

        transfer.getTransferItems().forEach(
                (transferItem) -> {
                    VoucherItem voucherItem = new VoucherItem();

                    // 获取商品的税率
                    PartyRole company = transfer.getOriginLocation().getCompany();
                    Tax tax = taxService.getSaleTax(company, transferItem.getProduct());
                    voucherItem.setTax(tax);

                    voucherItem.setProduct(transferItem.getProduct());
                    voucherItem.setOwner(transfer.getOriginLocation().getCompany());
                    voucherItem.setUom(transferItem.getUom());

                    // 判断是否为退回
                    if ( "reverse".equals( transfer.getTransferDirection() ) ) {
                        voucherItem.setProductQty( -1 * transferItem.getTransferQty());
                    } else {
                        voucherItem.setProductQty(transferItem.getTransferQty());
                    }

                    voucherItem.setPriceUnit(1);
                    voucherItem.setOriginItemId(String.valueOf(transferItem.getId()));
                    voucherItem.setSourceItemId(String.valueOf(transferItem.getId()));
                    voucherItems.add(voucherItem);
                }
        );
        return voucherItems;
    }
}
